//
//  AppSettings.swift
//  ZipTool
//
//  Created by jerry on 2025/8/15.
//

import Foundation
import SwiftUI

class AppSettings: ObservableObject {
    @Published var defaultExtractionPath: URL?
    @Published var showHiddenFiles: Bool = false
    @Published var preserveDirectoryStructure: Bool = true
    @Published var overwriteExistingFiles: Bool = false
    
    private let userDefaults = UserDefaults.standard
    
    init() {
        loadSettings()
    }
    
    private func loadSettings() {
        if let pathData = userDefaults.data(forKey: "defaultExtractionPath"),
           let path = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(pathData) as? URL {
            defaultExtractionPath = path
        }
        
        showHiddenFiles = userDefaults.bool(forKey: "showHiddenFiles")
        preserveDirectoryStructure = userDefaults.bool(forKey: "preserveDirectoryStructure")
        overwriteExistingFiles = userDefaults.bool(forKey: "overwriteExistingFiles")
    }
    
    func saveSettings() {
        if let path = defaultExtractionPath,
           let pathData = try? NSKeyedArchiver.archivedData(withRootObject: path, requiringSecureCoding: false) {
            userDefaults.set(pathData, forKey: "defaultExtractionPath")
        }
        
        userDefaults.set(showHiddenFiles, forKey: "showHiddenFiles")
        userDefaults.set(preserveDirectoryStructure, forKey: "preserveDirectoryStructure")
        userDefaults.set(overwriteExistingFiles, forKey: "overwriteExistingFiles")
    }
}

struct SettingsView: View {
    @ObservedObject var settings: AppSettings
    @State private var showingFolderPicker = false
    
    var body: some View {
        Form {
            Section("解压设置") {
                HStack {
                    Text("默认解压路径:")
                    Spacer()
                    if let path = settings.defaultExtractionPath {
                        Text(path.lastPathComponent)
                            .foregroundColor(.secondary)
                    } else {
                        Text("未设置")
                            .foregroundColor(.secondary)
                    }
                    Button("选择") {
                        showingFolderPicker = true
                    }
                }
                
                Toggle("保持目录结构", isOn: $settings.preserveDirectoryStructure)
                Toggle("覆盖已存在文件", isOn: $settings.overwriteExistingFiles)
            }
            
            Section("显示设置") {
                Toggle("显示隐藏文件", isOn: $settings.showHiddenFiles)
            }
        }
        .navigationTitle("设置")
        .fileImporter(
            isPresented: $showingFolderPicker,
            allowedContentTypes: [.folder]
        ) { result in
            switch result {
            case .success(let url):
                settings.defaultExtractionPath = url
                settings.saveSettings()
            case .failure:
                break
            }
        }
        .onChange(of: settings.preserveDirectoryStructure) { _ in
            settings.saveSettings()
        }
        .onChange(of: settings.overwriteExistingFiles) { _ in
            settings.saveSettings()
        }
        .onChange(of: settings.showHiddenFiles) { _ in
            settings.saveSettings()
        }
    }
}